﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;

namespace SocketSharp
{
    class Program
    {
        static Encoding? cp866 = null;

        static Encoding get866()
        {
            if (cp866 is null)
            {
                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                cp866 = Encoding.GetEncoding("CP866");
            }
            return cp866;
        }

        static void SendString(Socket s, string str)
        {
            s.Send(BitConverter.GetBytes(str.Length), sizeof(int), SocketFlags.None);
            s.Send(get866().GetBytes(str), str.Length, SocketFlags.None);
        }

        static string ReceiveString(Socket s)
        {
            byte[] b = new byte[sizeof(int)];

            s.Receive(b, sizeof(int), SocketFlags.None);
            int n = BitConverter.ToInt32(b, 0);
            b = new byte[n];
            s.Receive(b, n, SocketFlags.None);

            return get866().GetString(b, 0, n);
        }
        static void Main(string[] args)
        {
            int nPort = 12345;
            IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), nPort);
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            s.Connect(endPoint);
            if(s.Connected)
            {
                while(true)
                {
                    SendString(s, Console.ReadLine());
                    Console.WriteLine(ReceiveString(s));
                }
            }
        }
    }
}
